﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Wwtfly.Utility
{
    /// <summary>
    /// 文本 帮助
    /// </summary>
    public class ContentHelper
    {


        /// <summary>
        /// 转换成 是或否
        /// </summary>
        /// <returns></returns>
        public static string ConvertBoolState(bool state)
        {
            return state ? "是" : "否";
        }

        /// <summary>
        /// 转换成 是或否
        /// </summary>
        /// <returns></returns>
        public static string ConvertBoolState(bool? state)
        {
            return (state.HasValue ? state.Value : false) ? "是" : "否";
        }

        /// <summary>
        /// 截取方法
        /// </summary>
        /// <param name="obj">截取对象</param>
        /// <param name="len">最大字节数</param>
        /// <param name="bh">尾巴</param>
        /// <param name="mode">截取模式.(0,使用Length作为判断  1.一个汉字为2个长度的)</param>
        /// <returns></returns>
        public static string Substring(object obj, int len, string bh="...", int mode=0)
        {
            string result = string.Empty;
            if (obj != null)
            {
                string str = obj.ToString();
                if (mode == 0)
                {
                    if (str.Length > len)
                    {
                        result = (str.Substring(0, len - 1) + bh);
                    }
                    else
                    {
                        result = str;
                    }
                }
                else
                {
                    #region 一个汉字为2个长度的模块
                    string temp = string.Empty;
                    if (System.Text.Encoding.Default.GetByteCount(str) <= len) //如果长度比需要的长度n小,返回原字符串
                    {
                        result = str;
                    }
                    else
                    {
                        int t = 0;
                        char[] q = str.ToCharArray();
                        for (int i = 0; i < q.Length; i++)
                        {
                            if ((int)q[i] >= 0x4E00 && (int)q[i] <= 0x9FA5) //是否汉字
                            {
                                temp += q[i];
                                t += 2;
                            }
                            else
                            {
                                temp += q[i];
                                t += 1;
                            }
                            if (t >= len)
                            {
                                break;
                            }
                        }
                        result=(temp + bh);
                    }
                    #endregion
                }
            }
            return result;
        }



       /// <summary>
        ///  // 创建连接
       /// </summary>
       /// <param name="linkText"></param>
       /// <param name="url"></param>
       /// <returns></returns>
        public static string CreateA(string linkText, string url)
        {
            TagBuilder tbA = new TagBuilder("a");
            tbA.MergeAttribute("href", url);
            tbA.InnerHtml = linkText;
            return tbA.ToString(TagRenderMode.Normal);
        }
    }
}
